﻿//========================================================= 
/**@file MagPollCardTestCase.h
 * @brief 以轮询方式读取磁条的测试案例
 * 
 * @date 2017-07-29   11:42:52
 * @author Zhyioo 
 * @version 1.0
 */ 
//--------------------------------------------------------- 
#ifndef _LIBZHOUYB_MAGPOLLCARDTESTCASE_H_
#define _LIBZHOUYB_MAGPOLLCARDTESTCASE_H_
//--------------------------------------------------------- 
#include "../TestFrame.h"

#include "../../magnetic/H002MagDevAdapter.h"
using zhou_yb::application::magnetic::H002MagDevAdapter;
//--------------------------------------------------------- 
namespace zhou_yb {
namespace application {
namespace test {
//--------------------------------------------------------- 
/// H002轮询方式磁条读测试案例
struct MagPollCardTestCase : public InterruptBehavior, public ITestCase<IInteractiveTrans>
{
    /**
     * @brief 读磁条测试
     * @date 2017-07-29 09:55
     * 
     * @param [in] testObj 测试的接口对象
     * @param [in] testArg 磁条参数
     * - 参数:
     *  - Timeout : uint [default:DEV_WAIT_TIMEOUT] 等待刷磁的超时时间
     *  - Interval : uint [default:DEV_OPERATOR_INTERVAL] 轮询间隔
     *  - Tr1 : hex 需要比对的一磁道数据
     *  - Tr2 : hex 需要比对的二磁道数据
     *  - Tr3 : hex 需要比对的三磁道数据
     * .
     * @param [in] printer 信息输出器
     */
    virtual bool Test(Ref<IInteractiveTrans>& testObj, const ByteArray& testArg, TextPrinter& printer)
    {
        printer.TextPrint(TextPrinter::TextLogger, "轮询方式读取磁条测试");

        StringArguments arg;
        ArgParser parser;
        parser.Parse(testArg, arg);

        uint timeoutMs = arg["Timeout"].To<uint>(DEV_WAIT_TIMEOUT);
        uint intervalMs = arg["Interval"].To<uint>(DEV_OPERATOR_INTERVAL);

        // 需要比对的磁条数据

        H002MagDevAdapter mag;
        mag.SelectDevice(testObj);
        mag.SetWaitTimeout(timeoutMs);
        mag.SetOperatorInterval(intervalMs);
        mag.Interrupter = Interrupter;

        if(!mag.MagRead())
        {
            printer.TextPrint(TextPrinter::TextError, "读磁指令失败");
            return false;
        }
        ByteBuilder tr1(32);
        ByteBuilder tr2(128);
        ByteBuilder tr3(128);

        if(!mag.WaitMagCard(&tr1, &tr2, &tr3))
        {
            printer.TextPrint(TextPrinter::TextError, "刷磁失败");
            return false;
        }

        string sTr;
        ByteBuilder trBuff(128);
        if(arg.Get("Tr1", sTr))
        {
            trBuff.Clear();
            DevCommand::FromAscii(sTr.c_str(), trBuff);
            if(!tr1.IsEqual(trBuff))
            {
                printer.TextPrint(TextPrinter::TextError, "一磁数据比对失败");
                return false;
            }
        }
        if(arg.Get("Tr2", sTr))
        {
            trBuff.Clear();
            DevCommand::FromAscii(sTr.c_str(), trBuff);
            if(!tr1.IsEqual(trBuff))
            {
                printer.TextPrint(TextPrinter::TextError, "二磁数据比对失败");
                return false;
            }
        }
        if(arg.Get("Tr3", sTr))
        {
            trBuff.Clear();
            DevCommand::FromAscii(sTr.c_str(), trBuff);
            if(!tr1.IsEqual(trBuff))
            {
                printer.TextPrint(TextPrinter::TextError, "三磁数据比对失败");
                return false;
            }
        }
        return true;
    }
};
//--------------------------------------------------------- 
} // namespace test 
} // namespace application 
} // namespace zhou_yb
//--------------------------------------------------------- 
#endif // _LIBZHOUYB_MAGPOLLCARDTESTCASE_H_
//========================================================= 